home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / BlobMgr / Demo Folder / Fsh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  4.4 KB  |  202 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Blob Manager Demonstration:  Fish Puzzle module
  3.  *
  4.  * Cuts fish picture into pieces, allows user to reassemble.
  5.  *
  6.  * 26 July 1986    Paul DuBois
  7.  *
  8.  * 24 Dec 93
  9.  * - Added Answer button so user can see answer if unable to solve puzzle.
  10.  */
  11.  
  12. # include    "TransSkel.h"
  13.  
  14.  
  15. # include    "BlobMgr.h"
  16. # include    "BlobDemo.h"
  17.  
  18.  
  19. /*
  20.  * The picture is 234 wide by 144 high
  21.  */
  22.  
  23. # define    hOff        5    /* puzzle board offsets */
  24. # define    vOff        5
  25. # define    hWidth        18
  26. # define    vHeight        144
  27. # define    hGap        1
  28. # define    vGap        1
  29. # define    rows        1
  30. # define    cols        13
  31. # define    cells        13    /* i.e., rows x cols */
  32.  
  33.  
  34. static WindowPtr        wind;
  35. static GrafPtr            theFish;
  36. static BlobSetHandle    receptors;        /* receptor blobs */
  37. static BlobSetHandle    misc;
  38. static BlobHandle        scrambleBlob;    /* simulated control */
  39. static BlobHandle        answerBlob;        /* simulated control */
  40. static Boolean            paused;
  41.  
  42. static PicHandle        pic;
  43. static Rect                picRect;
  44.  
  45.  
  46. /*
  47.  * Make a receptor blob, glue it onto itself, and set it so that it
  48.  * is matched when it is glued onto itself.  This provides an easy
  49.  * way of telling when the fish has been reassembled properly after
  50.  * having been scrambled.
  51.  */
  52.  
  53. static void
  54. MakeRBlob (Rect *r)
  55. {
  56. BlobHandle    b;
  57. static short    pieceNo = 0;
  58. Rect        r2;
  59.  
  60.     b = NewBlob (receptors, true, infiniteGlue, true, 0L);
  61.     OpenBlob ();
  62.     r2 = *r;
  63.     OffsetRect (&r2, pieceNo * hWidth - r->left, -r->top);
  64.     CopyBits (&theFish->portBits, &wind->portBits, &r2, r, srcCopy, nil);
  65.     CloseRectBlob (b, r, r);
  66.     NewBlobMatch (b, b);
  67.     GlueGlob    (b, b);
  68.     ++pieceNo;
  69. }
  70.  
  71.  
  72. static void
  73. MakeBlobs (void)
  74. {
  75.     receptors = NewBlobSet ();
  76.     pic = (PicHandle) GetResource ('PICT', fshPictRes);
  77.     picRect    = (**pic).picFrame;
  78.     OffsetRect (&picRect, -picRect.left, -picRect.top);    /* to (0, 0) */
  79.     theFish = NewOffPort (&picRect);    /* draw fish into offscreen port */
  80.     SetPort    (theFish);
  81.     DrawPicture(pic, &picRect);
  82.     ReleaseResource    ((Handle) pic);
  83.     SetPort    (wind);
  84.     MakeBlobGrid (rows, cols, hOff, vOff, hWidth, vHeight,
  85.                     hGap, vGap, MakeRBlob);
  86.     DisposeOffPort (theFish);            /* throw away offscreen port */
  87. }
  88.  
  89.  
  90. static void
  91. Reset (void)
  92. {
  93.     ShuffleGlobSet (receptors);
  94.     HiliteBlob (scrambleBlob, inDragBlob, dimDraw);
  95.     HiliteBlob (answerBlob, inDragBlob, normalDraw);
  96.     paused = false;
  97. }
  98.  
  99.  
  100. static void
  101. Pause (void)
  102. {
  103.     HiliteBlob (scrambleBlob, inDragBlob, normalDraw);
  104.     HiliteBlob (answerBlob, inDragBlob, dimDraw);
  105.     paused = true;
  106. }
  107.  
  108.  
  109. static pascal void
  110. Mouse (Point pt, long t, short mods)
  111. {
  112. BlobHandle    b, d;
  113.  
  114.     if (TestBlob (scrambleBlob, pt) == inDragBlob)
  115.     {
  116.         if (BTrackMouse (scrambleBlob, pt, inFullBlob))
  117.             Reset ();
  118.     }
  119.     else if (TestBlob (answerBlob, pt) == inDragBlob)
  120.     {
  121.         if (BTrackMouse (answerBlob, pt, inFullBlob))
  122.         {
  123.             /* reassemble to show answer */
  124.             for (b = FirstBlob (receptors); b != (BlobHandle) nil; b = NextBlob (b))
  125.                 GlueGlob (b, b);
  126.             Pause ();
  127.         }
  128.     }
  129.     else if (!paused)
  130.     {
  131.         BlobClick (pt, t, nil, receptors);
  132.         if (BlobSetQuiet (receptors))
  133.             Pause ();
  134.     }
  135. }
  136.  
  137.  
  138. static pascal void
  139. Update (Boolean resized)
  140. {
  141. Rect    r;
  142.  
  143.     EraseRect (&wind->portRect);
  144.     SetRect (&r, 0, 0, 
  145.                 (hWidth + hGap) * cols + hGap + 4,
  146.                 vHeight + 2 * hGap + 4);
  147.     OffsetRect (&r, hOff-hGap-2, vOff-vGap-2);
  148.     PenMode (patBic);
  149.     FrameRect (&r);
  150.     PenNormal ();
  151.     DrawGrid (rows, cols, hOff, vOff, hWidth, vHeight, hGap, vGap);
  152.     DrawBlobSet (receptors);
  153.     DrawBlob (scrambleBlob, inFullBlob);
  154.     DrawBlob (answerBlob, inFullBlob);
  155. }
  156.  
  157.  
  158. static pascal void
  159. Activate (Boolean active)
  160. {
  161.     if (active)
  162.     {
  163.         SetDragRects (wind);
  164.         SetBCPermissions (false, false, false, true, true);
  165.     }
  166. }
  167.  
  168.  
  169. void
  170. FshInit (void)
  171. {
  172. Rect    r;
  173.  
  174.     SkelWindow (wind = GetDemoWind (fshWindRes),
  175.                 Mouse,            /* mouse clicks */
  176.                 nil,            /* key clicks */
  177.                 Update,            /* updates */
  178.                 Activate,        /* activate/deactivate events */
  179.                 nil,            /* close window */
  180.                 DoWClobber,        /* dispose of window */
  181.                 nil,            /* idle proc */
  182.                 false);            /* irrelevant, since no idle proc */
  183.  
  184.     BackPat (black);
  185.     EraseRect (&wind->portRect);
  186.     MakeBlobs ();
  187.  
  188.     misc = NewBlobSet ();
  189.     SetRect (&r, 0, 0, 20, 134);
  190.     OffsetRect (&r, wind->portRect.right - 46, 10);
  191.     scrambleBlob = NewVButtonBlob (misc, &r, "\pScramble", true);
  192.     HiliteBlob (scrambleBlob, inDragBlob, dimDraw);
  193.     OffsetRect (&r, 23, 0);
  194.     answerBlob = NewVButtonBlob (misc, &r, "\pAnswer", true);
  195.     HiliteBlob (answerBlob, inDragBlob, normalDraw);
  196.  
  197.     MakeFrontWind (wind);        /* show window */
  198.     SkelPause (30L);            /* wait a bit */
  199.     Reset ();                    /* scramble fish */
  200.     SkelDoUpdates ();            /* process update resulting from scramble */
  201. }
  202.